$(function() {
		$("#taskdatepicker").datepicker({minDate: 0, showOn: 'button', buttonImage: '/Smartodo/images/calendar.png', buttonImageOnly: true});
	});


function displayCalendar()
{
    // get the month selected and the year selected and navigate
    month = $("#mo").val();
    year = $("#yr").val();
    window.location = "Main.do?d=1&m="+month+"&y=" + year;
}

function displayToday()
{
    window.location = "Main.do?t=1";
}

function calendarDayDisplay(day, month, year)
{
    window.location = "Main.do?d=" + day + "&m=" + month + "&y=" + year;
}

function calendarNextMonth()
{
    window.location = "Main.do?n=1";
}

function calendarPrevMonth()
{
    window.location = "Main.do?p=1";
    //$.post("main.do", { p: "1"} );
}

function addTask()
{
	taskname = $("#newtaskname").val();
	if(taskname.length <=0 || taskname.length > 256)
	{
		alert("Please enter a valid task name.");
		return;
	} 

   	var data = $.ajax({
	  	url: "StodoActions.do",
		type: "POST",
		data: { 
		    service: 'addtask',
		    taskname: taskname,
		    date: $("#newtaskdate").val(),
		    priority: $("#newpriority").val()
		},
		async: false
	 }).responseText;
	
	if($('message', data).length > 0) 
	{
		var error_msg = $('error', data).text();
		if(error_msg != '')
		{
			alert(error_msg);
			return;
		}
	}
	// Refresh the page to get the updated task list
	$("#newtaskname").val("");
	window.location.reload();
}

function showUpdateTask(taskid)
{
   	var data = $.ajax({
	  	url: "StodoActions.do",
		type: "POST",
		data: { 
		    service: 'gettask',
		    taskid: taskid
		},
		async: false
	 }).responseText;

	if($('message', data).length > 0)
	{
		var error_msg = $('error', data).text();
		if(error_msg != '')
		{
			alert(error_msg);
			return;
		}
	}
	
	// hide the add task panel and show the update task panel
	$("#newTaskDiv").hide();
	$("#updateTaskDiv").show();
	
	if($('message', data).length > 0) 
	{
		$('message', data).each(function() {
				var taskid = $('taskid', data).text();
				$("#taskid").val(taskid);
				var taskname = $('taskname', data).text();
				$("#updatetaskname").val(taskname);
				var taskdate = $('taskdate', data).text();
				$("#taskdatepicker").val(taskdate);	
				var priority = $('priority', data).text();
				$("#updatepriority").val(priority);
			});
	}
}

function updateTask()
{
	taskname = $("#updatetaskname").val();
	if(taskname.length <=0 || taskname.length > 256)
	{
		alert("Please enter a valid task name.");
		return;
	} 
   	var data = $.ajax({
	  	url: "StodoActions.do",
		type: "POST",
		data: { 
		    service: 'updatetask',
		    taskid: $("#taskid").val(),
		    taskname: taskname,
		    date: $("#taskdatepicker").val(),
		    priority: $("#updatepriority").val()
		},
		async: false
	 }).responseText;
	
	if($('message', data).length > 0) 
	{
		var error_msg = $('error', data).text();
		if(error_msg != '')
		{
			alert(error_msg);
			return;
		}
	}
	
	// Refresh the page to get the updated task list
	window.location.reload();
}

function deleteTask()
{
	if(!confirm("Are you sure to delete this task?"))
		return;
		
   	var data = $.ajax({
	  	url: "StodoActions.do",
		type: "POST",
		data: { 
		    service: 'deletetask',
		    taskid: $("#taskid").val()
		},
		async: false
	 }).responseText;
	
	if($('message', data).length > 0) 
	{
		var error_msg = $('error', data).text();
		if(error_msg != '')
		{
			alert(error_msg);
			return;
		}
	}
	
	// Refresh the page to get the updated task list
	window.location.reload();
}

function cancelUpdate()
{
	// hide the update task panel and show the new task panel
	$("#newTaskDiv").show();
	$("#updateTaskDiv").hide();
}

